package com.py.korona.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import com.py.korona.db.model.CategoriesInRoomEntity;
import com.py.korona.db.model.CategoryEntity;
import com.py.korona.db.model.GeoplanEntity;
import com.py.korona.db.model.GeoplanPerimetrEntity;
import com.py.korona.db.model.GeopointEntity;
import com.py.korona.db.model.GeopointsInRoom;
import com.py.korona.db.model.RoomEntity;
import com.py.korona.db.model.RoomInGeoplan;
import com.py.korona.db.model.base.EntityDescription;
import com.py.korona.db.model.base.EntityProperty;

public class DBHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME      = "test_db.db";
    private static final int   DATABASE_VERSION   = 25;
    public final static int    MESSAGE_DB_UPDATED = 0;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public DBHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(createTableScriptForTableName(CategoryEntity.entityTableName(), CategoryEntity.entityDescription()));
        database.execSQL(createTableScriptForTableName(RoomEntity.entityTableName(), RoomEntity.entityDescription()));
        database.execSQL(createTableScriptForTableName(CategoriesInRoomEntity.entityTableName(), CategoriesInRoomEntity.entityDescription()));
        database.execSQL(createTableScriptForTableName(GeopointEntity.entityTableName(), GeopointEntity.entityDescription()));
        database.execSQL(createTableScriptForTableName(GeopointsInRoom.entityTableName(), GeopointsInRoom.entityDescription()));
        database.execSQL(createTableScriptForTableName(GeoplanEntity.entityTableName(), GeoplanEntity.entityDescription()));
        database.execSQL(createTableScriptForTableName(GeoplanPerimetrEntity.entityTableName(), GeoplanPerimetrEntity.entityDescription()));
        database.execSQL(createTableScriptForTableName(RoomInGeoplan.entityTableName(), RoomInGeoplan.entityDescription()));
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        /** Category entity */
        String script = dropTableScriptForTableName(CategoryEntity.entityTableName());
        database.execSQL(script);
        script = createTableScriptForTableName(CategoryEntity.entityTableName(), CategoryEntity.entityDescription());
        database.execSQL(script);
        /** Room entity */
        script = dropTableScriptForTableName(RoomEntity.entityTableName());
        database.execSQL(script);
        script = createTableScriptForTableName(RoomEntity.entityTableName(), RoomEntity.entityDescription());
        database.execSQL(script);
        /** RoomCategories entity */
        script = dropTableScriptForTableName(CategoriesInRoomEntity.entityTableName());
        database.execSQL(script);
        script = createTableScriptForTableName(CategoriesInRoomEntity.entityTableName(), CategoriesInRoomEntity.entityDescription());
        database.execSQL(script);
        /** Geopoint entity */
        script = dropTableScriptForTableName(GeopointEntity.entityTableName());
        database.execSQL(script);
        script = createTableScriptForTableName(GeopointEntity.entityTableName(), GeopointEntity.entityDescription());
        database.execSQL(script);
        /** GeopointsInRoom entity */
        script = dropTableScriptForTableName(GeopointsInRoom.entityTableName());
        database.execSQL(script);
        script = createTableScriptForTableName(GeopointsInRoom.entityTableName(), GeopointsInRoom.entityDescription());
        database.execSQL(script);
        /** Geoplan entity */
        script = dropTableScriptForTableName(GeoplanEntity.entityTableName());
        database.execSQL(script);
        script = createTableScriptForTableName(GeoplanEntity.entityTableName(), GeoplanEntity.entityDescription());
        database.execSQL(script);
        /** Geoperimetr entity */
        script = dropTableScriptForTableName(GeoplanPerimetrEntity.entityTableName());
        database.execSQL(script);
        script = createTableScriptForTableName(GeoplanPerimetrEntity.entityTableName(), GeoplanPerimetrEntity.entityDescription());
        database.execSQL(script);
        /** RoominGeoplan entity */
        script = dropTableScriptForTableName(RoomInGeoplan.entityTableName());
        database.execSQL(script);
        script = createTableScriptForTableName(RoomInGeoplan.entityTableName(), RoomInGeoplan.entityDescription());
        database.execSQL(script);
    }

    private String createTableScriptForTableName(String tableName, EntityDescription entityDescription) {
        String columnList = new String();
        for (EntityProperty column : entityDescription.properties()) {
            if (TextUtils.isEmpty(columnList)) {
                columnList = column.asSQLDefinition();
            } else {
                columnList += "," + column.asSQLDefinition();
            }
        }
        String result = "create table " + tableName + "(" + columnList + ");";
        return result;
    }

    private String dropTableScriptForTableName(String tableName) {
        return "drop table if exists " + tableName;
    }
}
